package fr.ema.arch3d.gea.spec;

//~--- non-JDK imports --------------------------------------------------------

import java.util.ArrayList;
import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;

import org.jgraph.JGraph;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;
import org.objectweb.fractal.api.NoSuchInterfaceException;

import fr.ema.arch3d.gea.port.MyCellViewFactory;
import fr.ema.arch3d.model.architecture.specification.ComponentRole;
import fr.ema.arch3d.model.architecture.specification.Connection;
import fr.ema.arch3d.model.architecture.specification.Specification;

public class DrawSpecImpl {
    public static JGraph      graph;
    public static JScrollPane sGraph;
    public static JTextPane   sText;
    public static JScrollPane sTree;
    public List               cmpRole_list = new ArrayList();
    public Specification      spec;

    public JSplitPane drawSpec(Specification spec) {
        this.spec = spec;

        GraphModel model = new DefaultGraphModel();

        /*
         * GraphLayoutCache vieww = new GraphLayoutCache(modell,
         * new DefaultCellViewFactory());
         */
        GraphLayoutCache view = new GraphLayoutCache(model, (new MyCellViewFactory()));

        graph = new JGraph(model, view);

        DefaultGraphCell[] cells = null;
        MyEdge[]           edges = null;

        // draw vertext
        DrawVertex dv = new DrawVertex();

        try {
            cmpRole_list = spec.getCmpRoleList();

            // System.out.println("(("+((ComponentRole)cmpRole_list.get(0)).cmpType.name);
            cells = new DefaultGraphCell[cmpRole_list.size()];

            for (int i = 0; i < cells.length; i++) {
                cells[i] = dv.drawVertext((ComponentRole) cmpRole_list.get(i), i);

                // System.out.println("(("+((ComponentRole)cmpRole_list.get(0)).cmpType.name);
                // GraphConstants.setAutoSize(cells[i].getAttributes(),true);
            }
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // draw edges
        DrawEdge de              = new DrawEdge();
        List     connection_list = spec.getConnectionList();

        edges = new MyEdge[connection_list.size()];

        for (int i = 0; i < edges.length; i++) {
            edges[i] = de.drawEdge(cells, (Connection) connection_list.get(i));
        }

        graph.getGraphLayoutCache().insert(cells);
        graph.getGraphLayoutCache().insert(edges);
        graph.setPortsVisible(true);
        sGraph = new JScrollPane(graph);

        // MyGraphTreeModel gtModel = new MyGraphTreeModel(graph.getModel());
        MyTreeSpec tree = new MyTreeSpec(model);

        // graph.getModel().addGraphModelListener(gtModel);
        // tree.setRootVisible(false);
        sTree = new JScrollPane(tree.s);

        ADLTextSpec t = new ADLTextSpec();

        sText = new JTextPane();;

        // sTextPane=t.createTextPane(spec);
 //       sText.setDocument(t.createTextSpec(spec));
        sText.setEditable(false);

        JSplitPane pane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sTree, sGraph);
        JSplitPane pane  = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pane1, sText);

        pane.setSize(1100, 300);
        pane1.setDividerLocation(180);
        pane.setDividerLocation(900);

        // JSplitPane pane1 =new JSplitPane(JSplitPane.VERTICAL_SPLIT, null, pane);
        // set mouse listener
        graph.addMouseListener(new MouseEventSpec(this));

        return pane;
    }
}


 
